package org.lynn.montecarlo;

import java.awt.*;

/**
 * @author gy
 * @date 2019/6/22 11:23
 */
public class AlgoVisualizer {

    private static final Integer DELAY = 40;

    private MonterCarloData data;
    private AlgoFrame frame;
    private int N;
    private int insideCircle;

    public AlgoVisualizer(int sceneWidth, int sceneHeight, int N) {

        if (sceneWidth != sceneHeight) {
            throw new IllegalArgumentException("This demo must be run in a square window!");
        }

        // 初始化数据
        this.N = N;
        Circle circle = new Circle(sceneWidth / 2, sceneHeight / 2, sceneWidth / 2);
        data = new MonterCarloData(circle);

        // 初始化视图
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Get Pi With Monte Carlo", sceneWidth, sceneHeight);
            new Thread(() -> {
                run();
            }).start();
        });
    }


    /**
     * 动画逻辑
     */
    private void run() {
        for (int i = 0; i < N; i++) {

            if (i % 100 == 0) {
                frame.render(data);
                AlgoVisHelper.pause(DELAY);
                System.out.println(data.estimatePi());
            }

            int x = (int) (Math.random() * frame.getCanvasWidth());
            int y = (int) (Math.random() * frame.getCanvasHeight());
            data.addPoint(new Point(x, y));

        }
    }


    public static void main(String[] args) {

        int sceneWidth = 800;
        int sceneHeight = 800;

        AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, 10000);
    }
}

